/**
 * 认证相关类型定义
 */

// ============ 基础类型 ============

export interface BaseEntity {
  id: number;
  created_at: string;
  updated_at: string;
}

// ============ 用户相关类型 ============

export interface User extends BaseEntity {
  username: string;
  email: string;
  full_name?: string;
  avatar_url?: string;
  phone?: string;
  department?: string;
  position?: string;
  is_active: boolean;
  is_superuser: boolean;
  last_login?: string;
  roles: Role[];
}

export interface UserProfile {
  id: number;
  username: string;
  email: string;
  full_name?: string;
  avatar_url?: string;
  phone?: string;
  department?: string;
  position?: string;
  is_active: boolean;
  is_superuser: boolean;
  last_login?: string;
  created_at: string;
  updated_at: string;
  roles: Role[];
  permissions: Permission[];
}

export interface UserCreate {
  username: string;
  email: string;
  password: string;
  full_name?: string;
  phone?: string;
  department?: string;
  position?: string;
  role_ids?: number[];
}

export interface UserUpdate {
  email?: string;
  full_name?: string;
  phone?: string;
  department?: string;
  position?: string;
  is_active?: boolean;
  role_ids?: number[];
}

// ============ 角色相关类型 ============

export interface Role extends BaseEntity {
  name: string;
  display_name: string;
  description?: string;
  is_system: boolean;
  is_active: boolean;
  permissions: Permission[];
}

export interface RoleCreate {
  name: string;
  display_name: string;
  description?: string;
  permission_ids?: number[];
}

export interface RoleUpdate {
  display_name?: string;
  description?: string;
  is_active?: boolean;
  permission_ids?: number[];
}

// ============ 权限相关类型 ============

export interface Permission extends BaseEntity {
  name: string;
  display_name: string;
  description?: string;
  resource: string;
  action: string;
  is_system: boolean;
}

export interface PermissionCreate {
  name: string;
  display_name: string;
  description?: string;
  resource: string;
  action: string;
}

export interface PermissionUpdate {
  display_name?: string;
  description?: string;
}

// ============ 认证相关类型 ============

export interface LoginRequest {
  username: string;
  password: string;
  remember_me?: boolean;
}

export interface LoginResponse {
  access_token: string;
  refresh_token: string;
  token_type: string;
  expires_in: number;
  user: UserProfile;
}

export interface RegisterRequest {
  username: string;
  email: string;
  password: string;
  confirm_password: string;
  full_name?: string;
  phone?: string;
  department?: string;
  position?: string;
  role_ids?: number[];
}

export interface RegisterResponse {
  message: string;
  user: User;
}

export interface ChangePasswordRequest {
  current_password: string;
  new_password: string;
  confirm_password: string;
}

export interface ResetPasswordRequest {
  email: string;
}

export interface TokenRefreshRequest {
  refresh_token: string;
}

export interface TokenResponse {
  access_token: string;
  refresh_token?: string;
  token_type: string;
  expires_in: number;
}

// ============ 认证状态类型 ============

export interface AuthState {
  isAuthenticated: boolean;
  user: UserProfile | null;
  token: Token | null;
  permissions: string[];
  loading: boolean;
  error: string | null;
}

export interface Token {
  access_token: string;
  refresh_token: string;
  token_type: string;
  expires_in: number;
  expires_at: number;
}

// ============ 权限检查类型 ============

export interface PermissionCheckRequest {
  permissions: string[];
  require_all?: boolean;
}

export interface PermissionCheckResponse {
  has_permission: boolean;
  missing_permissions: string[];
}



// ============ 列表响应类型 ============

export interface UserListResponse {
  users: User[];
  total: number;
  page: number;
  page_size: number;
  total_pages: number;
}

export interface RoleListResponse {
  roles: Role[];
  total: number;
  page: number;
  page_size: number;
  total_pages: number;
}

export interface PermissionListResponse {
  permissions: Permission[];
  total: number;
  page: number;
  page_size: number;
  total_pages: number;
}

export interface OperationLogListResponse {
  logs: OperationLog[];
  total: number;
  page: number;
  page_size: number;
  total_pages: number;
}

// ============ 表单验证类型 ============

export interface PasswordStrength {
  is_valid: boolean;
  score: number;
  strength: 'weak' | 'medium' | 'strong' | 'very_strong';
  feedback: string[];
  requirements: {
    length: boolean;
    uppercase: boolean;
    lowercase: boolean;
    digit: boolean;
    special: boolean;
  };
}

// ============ 错误类型 ============

export type AuthErrorType = 
  | 'INVALID_CREDENTIALS'
  | 'USER_NOT_FOUND'
  | 'USER_INACTIVE'
  | 'USER_LOCKED'
  | 'TOKEN_EXPIRED'
  | 'TOKEN_INVALID'
  | 'PERMISSION_DENIED'
  | 'NETWORK_ERROR'
  | 'SERVER_ERROR'
  | 'VALIDATION_ERROR';

export interface AuthError {
  type: AuthErrorType;
  message: string;
  details?: any;
}

// ============ 菜单权限类型 ============

export interface MenuItem {
  key: string;
  label: string;
  icon?: React.ReactNode;
  path?: string;
  children?: MenuItem[];
  permissions?: string[];
  roles?: string[];
  hidden?: boolean;
}

// ============ 路由守卫类型 ============

export interface RouteGuard {
  permissions?: string[];
  roles?: string[];
  requireAuth?: boolean;
  requireAll?: boolean;
}

// ============ 会话信息类型 ============

export interface SessionInfo {
  id: number;
  user_id: number;
  device_info?: any;
  ip_address?: string;
  user_agent?: string;
  is_active: boolean;
  expires_at: string;
  last_activity: string;
  created_at: string;
}

// ============ 用户状态枚举 ============

export enum UserStatus {
  ACTIVE = 'active',
  INACTIVE = 'inactive',
  LOCKED = 'locked'
}

export enum OperationStatus {
  SUCCESS = 'SUCCESS',
  FAILED = 'FAILED',
  ERROR = 'ERROR'
}

// ============ 常量定义 ============

export const AUTH_STORAGE_KEYS = {
  TOKEN: 'auth_token',
  REFRESH_TOKEN: 'refresh_token',
  USER_INFO: 'user_info',
  PERMISSIONS: 'user_permissions',
  REMEMBER_ME: 'remember_me'
} as const;

export const PASSWORD_REQUIREMENTS = {
  MIN_LENGTH: 8,
  REQUIRE_UPPERCASE: true,
  REQUIRE_LOWERCASE: true,
  REQUIRE_DIGITS: true,
  REQUIRE_SPECIAL: true
} as const;

export const DEFAULT_PERMISSIONS = {
  LINEAGE_VIEW: 'lineage.view',
  TASK_VIEW: 'task.view',
  USER_VIEW: 'user.view',
  USER_MANAGE: 'user.manage',
  ROLE_VIEW: 'role.view',
  ROLE_MANAGE: 'role.manage',
  SYSTEM_CONFIG: 'system.config'
} as const;
